﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Input;
using System.Xml;

namespace WebFlipScreenSaver.Model
{
	[Serializable]
	[DataContract]
	public class SettingsViewModel : NotifyPropertyChangedBase
	{
		#region Commands
		
		private ICommand _addConfigCommandCommand;

		public ICommand AddConfigCommand
		{
			get
			{
				return _addConfigCommandCommand ??
				       (_addConfigCommandCommand = new RelayCommand(x =>
				                                     	{
															SelectedWebSiteConfiguration = new WebPageConfigurationViewModel();
															WebsiteConfigurations.Add(SelectedWebSiteConfiguration);

				                                     	}));
			}
		}

		private ICommand _saveConfigCommandCommand;

		public ICommand SaveConfigCommand
		{
			get
			{
				return _saveConfigCommandCommand ??
				       (_saveConfigCommandCommand = new RelayCommand(x => FlipperSettings.SaveSettings(this)));
			}
		}

		private ICommand _deleteConfigCommandCommand;

		public ICommand DeleteConfigCommand
		{
			get
			{
				return _deleteConfigCommandCommand ??
				       (_deleteConfigCommandCommand = new RelayCommand(x =>
				                                     	{
															if (SelectedWebSiteConfiguration != null)
															{
																WebsiteConfigurations.Remove(SelectedWebSiteConfiguration);
																SelectedWebSiteConfiguration = null;
															}

				                                     	}));
			}
		}
		#endregion

		#region Bindable Properties
		
		private bool _canDelete;

		public bool CanDelete
		{
			get { return _canDelete; }
			set
			{
				_canDelete = value;
				Changed(() => CanDelete);
			}
		}

		private bool _canSaveConfig;

		public bool CanSaveConfig
		{
			get { return _canSaveConfig; }
			set
			{
				_canSaveConfig = value;
				Changed(() => CanSaveConfig);
			}
		}

		private ObservableCollection<WebPageConfigurationViewModel> _websiteConfigurations = new ObservableCollection<WebPageConfigurationViewModel>();
		[DataMember]
		public ObservableCollection<WebPageConfigurationViewModel> WebsiteConfigurations
		{
			get { return _websiteConfigurations; }
			set
			{
				_websiteConfigurations = value;
				Changed(() => WebsiteConfigurations);
				
			}
		}

		private WebPageConfigurationViewModel _selectedWebSiteConfiguration;
		public WebPageConfigurationViewModel SelectedWebSiteConfiguration
		{
			get { return _selectedWebSiteConfiguration; }
			set
			{
				_selectedWebSiteConfiguration = value;
				Changed(() => SelectedWebSiteConfiguration);
				CanDelete = value != null;
			}
		}
		#endregion
	}
}
